package com.star.starp.utils;

import com.star.starp.config.SystemConfig;
import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.teaopenapi.models.Config;

/**
 * @author alex
 * @date 2022/1/2 17:04
 * @description:TODO
 */
public class AliSmsUtils {
    private static Client client;
    //完成初始化
    static {
        Config config = new Config()
                // 您的AccessKey ID
                .setAccessKeyId(SystemConfig.ALI_KEY)
                // 您的AccessKey Secret
                .setAccessKeySecret(SystemConfig.ALI_SEC);
        // 访问的域名
        config.endpoint = "dysmsapi.aliyuncs.com";
        try {
            client=new Client(config);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * 发送验证码
     * @param code 验证码
     * @param phone 手机号
     * @param temcode 模板code*/
    public static boolean sendCode(String temcode,String phone,int code){
        //1.准备请求参数
        SendSmsRequest sendSmsRequest = new SendSmsRequest()
                .setPhoneNumbers(phone)
                .setSignName("来自邢朋辉的短信")
                .setTemplateCode(temcode)
                .setTemplateParam("{\"code\":"+code+"}");
        try {
            //2.发送短信，获取响应结果
            SendSmsResponse response=client.sendSms(sendSmsRequest);
            //3.返回结果
            return response.getBody().code.equals("OK");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
}
